package com.gxa.p2p.service.impl;
import com.gxa.p2p.mapper.LoginLogMapper;
import com.gxa.p2p.mapper.UserInfoMapper;
import com.gxa.p2p.mapper.LoginLogMapper;
import com.gxa.p2p.mapper.UserInfoMapper;
import com.gxa.p2p.mapper.UserRegisterMapper;
import com.gxa.p2p.pojo.AccountInfo;
import com.gxa.p2p.pojo.LoginLog;
import com.gxa.p2p.pojo.UserInfo;
import com.gxa.p2p.pojo.UserRegister;
import com.gxa.p2p.service.AccountInfoService;
import com.gxa.p2p.service.Userservice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Service("userService")//自动扫描路径下面的包
@Transactional
public class UserServiceImpl implements Userservice {
    @Resource//默认按byName自动注入 跟@Autowired一样 自动装配 @Autowired是按byType注入.
    UserRegisterMapper userRegisterMapper=null;
    @Resource
    LoginLogMapper loginLogMapper;
    @Resource
    UserInfoMapper userInfoMapper;
    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED,
            rollbackFor={Throwable.class})//事务隔离级别
    /**
     * 登录操作
     * @param
     * @return
     */
    public UserRegister login(String username, String password, HttpServletRequest request) {

        int sum=0; //记录影响行数
        UserRegister userRegister = userRegisterMapper.login(username, password);
        if(userRegister!=null){
           sum++;
       }
       //找出新增用户信息
         UserRegister username1 = userRegisterMapper.findUsername(username);
        // 创建一个用户信息对象
        LoginLog loginLog=new LoginLog();
        // 将添加成功的用户的id设置到用户信息表的loginLogId
        loginLog.setUserId(username1.getId());
        //传入id,传入ip
      sum+=loginLogMapper.insert(username1.getId(),request.getRemoteAddr());
        // 使用生成的条件插入SQL
        if (sum==2){
        return userRegister;
    }else {
        return null;
    }
        //返回操作结果
        //return null;
        //UserRegister login = userRegisterMapper.login(username, password);
        //return login!=null;

    }




    /**
     * 注册操作
     * @param
     * @return
     */
    @Override
    public boolean save(UserRegister userRegister) {
        int sum =0;// 记录影响行数
        sum+=userRegisterMapper.save(userRegister);
        // 找出新增的用户信息
        UserRegister username = userRegisterMapper.findUsername(userRegister.getUserName());
        // 创建一个用户信息对象
        UserInfo userInfo = new UserInfo();
        // 将添加成功的用户的id设置到用户信息表的userId
        userInfo.setUserId(username.getId());
        // 将注册时的手机号存入用户信息表
        userInfo.setPhoneNumber(userRegister.getPhoneNumber());
        // 使用生成的条件插入SQL
        sum+=userInfoMapper.insertSelective(userInfo);
        // 返回操作结果
        return sum==2;
    }
    /**
     * 查询用户名是否重复
     * @param
     * @return
     */
    @Override
    //重写方法
    public boolean findByName(String username) {
        UserRegister username1 = userRegisterMapper.findUsername(username);
        return username1!=null;//返回判断boolean值
    }
    /**
     * 根据id 和密码来修改密码
     * @param
     * @return
     */
    @Override
    public boolean upDateP(String phonenumber,String password) {
         //UserRegister userRegister = userRegisterMapper.upDataP(id,password)
        return userRegisterMapper.upDataP(phonenumber,password)>0;//除了查询操作都是返回操作影响的行数.
    }
    /**
     * 查询是否有电话号并且发送验证码
     * @param
     * @return
     */
    @Override
    public boolean findPhoneNumber(String phonenumber) {
        UserRegister phoneNumber = userRegisterMapper.findPhoneNumber(phonenumber);
        return phoneNumber!=null;
    }


}
